#include "player.h"

#include "camera.h"
#include "snowball.h"
#include "lightbeamdamager.h"
#include "terrain.h"
#include "hat.h"

class CSnowman;
class Helicopter;


CPlayer::CPlayer()
{
	size = 7.0f;
	camera = NULL;
	terrain = NULL; 
	audioSys = NULL;
	isFiringLightbeam = false;
	lightbeamDamageTimeout = 0.0f;
	clock_t start = clock();
}

CPlayer::~CPlayer() {}


void CPlayer::OnPrepare()
{
	if (camera)
	{
		position = camera->position;		// the player stays with the camera
		direction = camera->yaw;
		pitch = camera->pitch;
	}
}


void CPlayer::OnCollision(CObject *collisionObject)
{
	if (typeid(*collisionObject) == typeid(CTerrain))
	{
		position.y = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size;
	}
	else if (typeid(*collisionObject) == typeid(CSnowman))
	{
		if (camera)
		{
			audioSys->PlaySoundDX(audioSys->iMonsterBite);
			isDead = true;
		}
	}
	else if (typeid(*collisionObject) == typeid(Helicopter))
	{
	}
	else if (typeid(*collisionObject) == typeid(Hat))
	{
	}
	else if (typeid(*collisionObject) == typeid(CPlayer))
	{
	}
}


void CPlayer::SetCamera(CCamera *c)
{
	camera = c;
}


void CPlayer::DetachCamera()
{
	camera = NULL;
}


void CPlayer::SetTerrain(CTerrain *t)
{
	terrain = t;
}

void CPlayer::StartWeapon()
{
	isFiringLightbeam = true;
	camera->enableMovement = false;
	float lightbeamDamageTimeout = 0.0f;
}

void CPlayer::EndWeapon()
{
	camera->enableMovement = true;
	isFiringLightbeam = false;
}

void CPlayer::OnWeaponFire()
{
	// use camera->lookAt vector and multiply
	// this lookAt vector by the ammo's speed
	// to get the ammo velocity vector.
	// create the new ammo and attach it to
	// the world. fire the ammo
	CLightBeamDamager *newLightBeamDamager = new CLightBeamDamager;
	
	newLightBeamDamager->pitch = camera->pitch;
	newLightBeamDamager->direction = direction;
	newLightBeamDamager->position = position;
	newLightBeamDamager->forward = camera->lookAt.UnitVector();
	newLightBeamDamager->SetAudioSystem(audioSys);
	newLightBeamDamager->AttachTo(terrain);
	newLightBeamDamager->SetPlayer(this);
}

void CPlayer::DoAnimate(float deltaTime) {
	if (isFiringLightbeam) {

		audioSys->PlaySoundDX(audioSys->iLaserBeam);

		lightbeamDamageTimeout -= deltaTime;

		if (lightbeamDamageTimeout < 0) {
			OnWeaponFire();
			lightbeamDamageTimeout += 0.075f;
		}
	}
}

void CPlayer::LaunchSnowBall()
{
	CSnowBall *newSnowball = new CSnowBall;

	audioSys->PlaySoundDX(audioSys->iSnowBallThrow);

	newSnowball->pitch = camera->pitch;
	newSnowball->direction = direction;
	newSnowball->position = position;
	newSnowball->forward = camera->lookAt.UnitVector();
	newSnowball->SetAudioSystem(audioSys);
	newSnowball->LoadAudio(audioSys->iSnowBallSplat);
	newSnowball->AttachTo(terrain);
	newSnowball->SetPlayer(this);
}


void CPlayer::SetAudioSystem(CDirectXAudio *aSys)
{
	audioSys = aSys;
}